<template lang="html">
  <div class="el-container content"
   v-loading="loading"
   element-loading-text="正在单点登录"
   >
  </div>
</template>

<script>
import axios from 'axios'
import storage from '@/assets/js/storage'
import { mapGetters, mapMutations } from 'vuex'
import util from '@/assets/js/util';
import Cookies from 'js-cookie'
export default {
  name: 'login',
  data() {
    return {
      loading: true,
      code: "",
      authorize: 'https://id.chinaoct.com:28080/bim-webservice/api/identity/authorize?client_id=sikushangbao&client_secret=f0993ca02f7442c7af0c57c5bb4f267b&state=123&redirect_uri=' + encodeURIComponent(window.location.href)
    }
  },
  computed: {
    ...mapGetters({
      userInfo: 'getUserInfo',
    }),
  },
  methods: {
    ...mapMutations(['changeUserInfo']),
    createToken() {
      let params = {
        code: this.code,
        ts: Date.now(),
      }
      let url = '/station/createToken'
      axios
        .get(url, {
          params: params,
        })
        .then((data) => {
          let result = data.data
          if (result.status === 'SUCCESS') {
            this.changeUserInfo(result.content)
            storage.setSession('login_userinfo', JSON.stringify(result.content))
            Cookies.set('station-token', result.content.token, { expires: 1 })
            this.$router.replace({ path: '/shangbao' })
          } else {
            this.$message({
              showClose: true,
              message: '单点登录失败，请重新打开应用',
              type: 'error',
            })
          }
          this.loading = false
        })
        .catch((err) => {
          // eslint-disable-next-line no-console
          console.log(err)
        })
    },
  },
  mounted() {
    this.code=util.getSearch('code')
    if (this.code) {
      this.createToken()
    }else {
      window.open(this.authorize, '_self')
    }
  },
}
</script>

<style lang="css" >
</style>
